package org.yx.service.sres.save_device_resource;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import org.yx.exception.BizException;
import org.yx.models.SresDevDef;
import org.yx.utils.BaseService;
import org.yx.utils.FieldMap;

import yxmes.tools.FineReportUtil;

import com.fr.general.FRFont;
import com.fr.general.ModuleContext;
import com.fr.io.exporter.ExcelExporter;
import com.fr.main.impl.WorkBook;
import com.fr.main.workbook.ResultWorkBook;
import com.fr.report.module.EngineModule;
import com.fr.stable.WriteActor;
import com.xiaoleilu.hutool.io.FileUtil;
import com.xiaoleilu.hutool.poi.excel.ExcelUtil;
import com.xiaoleilu.hutool.util.StrUtil;

/**
 * 保存设备资源台账（新增，修改，删除操作）
 * 
 * @author zxw
 * @since 2019/06/12
 **/
public class SresSaveDeviceResourceService extends BaseService {
	public SresSaveDeviceResourceOut saveDeviceResource(SresSaveDeviceResourceIn in) throws Exception {
		if (StrUtil.isBlank(in.getResId())) {
			throw new BizException("SYS-0004", FieldMap.of("parameter", "resId"));
		}

		String fileId = null;
		if (in.getResStatus() != null && 'D' == in.getResStatus()) {
			// delete
			SresDevDef res = selectOneLocked(SresDevDef.class, in.getUserOrgCode(), in.getResId());
			if (res == null || res.getResStatus() == 'D') {
				throw new BizException("SRES-0001", "此资源${resId}不存在或已经删除。", FieldMap.of("resId", in.getResId()));
			}

			res.setResStatus('D');

			File tmp = FineReportUtil.createDownloadFile("xls");
			ExcelUtil.createBook(tmp).close();
			System.out.println(tmp.getAbsolutePath());
			OutputStream outputStream = null;
			try {
				outputStream = new FileOutputStream(tmp);
				String filePath = "doc\\bh\\test\\res.cpt";

				ModuleContext.startModule(EngineModule.class.getName());
				WorkBook frWb = (WorkBook) FineReportUtil.readTemplate(filePath);

				Map<String, Object> mapParm = new HashMap<String, Object>();
				mapParm.put("resId", res.getResId());

				ExcelExporter ExcelExport = new ExcelExporter();
				ResultWorkBook rwb = frWb.execute(mapParm, new WriteActor());
				rwb.getReport(0).getFooter(0).addLeftText("A", FRFont.getInstance());
				ExcelExport.export(outputStream, rwb);

				fileId = tmp.getName();
			} catch (Exception e) {
				if (outputStream != null) {
					outputStream.close();
					outputStream = null;
				}
				FileUtil.del(tmp);
				throw e;
			} finally {
				if (outputStream != null) {
					outputStream.close();
				}
				ModuleContext.stopModules();
			}

			// update(res, "resStatus");

		} else {
			// insert or update
			SresDevDef res = selectOneLocked(SresDevDef.class, in.getUserOrgCode(), in.getResId());
			if (res != null) {
				if (res.getResStatus() == 'D') {
					throw new BizException("SRES-0001", "此资源${resId}不存在或已经删除。", FieldMap.of("resId", in.getResId()));
				}

				copyProperties(in, res);

				update(res);
			} else {
				res = new SresDevDef();
				copyProperties(in, res);
				res.setOrgCode(in.getUserOrgCode());
				res.setResStatus('N');

				insert(res);
			}
		}

		SresSaveDeviceResourceOut out = new SresSaveDeviceResourceOut();
		out.setFileId(fileId);
		return out;
	}
}